<?php

namespace App\Command\Make;

use Hyperf\Devtool\Generator\GeneratorCommand as Command;

abstract class GeneratorCommand extends Command
{
    /**
     * Replace the namespace for the given stub.
     *
     * @param string $stub
     * @param string $name
     * @return $this
     */
    protected function replaceNamespace(&$stub, $name): static
    {
        $stub = str_replace(
            ['{{ namespace }}', '{{ rootNamespace }}'],
            [$this->getNamespace($name), $this->rootNamespace()],
            $stub
        );
        return $this;
    }

    /**
     * Replace the class name for the given stub.
     *
     * @param string $stub
     * @param string $name
     * @return string
     */
    protected function replaceClass($stub, $name): string
    {
        $class = str_replace($this->getNamespace($name) . '\\', '', $name);

        return str_replace('{{ class }}', $class, $stub);
    }

    protected function rootNamespace(): string
    {
        return 'App\\';
    }
}